﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LoadTests.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// <summary>
//   A set of  tests.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Tests
{
  using System;
  using System.Linq;

  using DataDuctus.Snmp.Persistor.Core.Persistor;
  using DataDuctus.Snmp.Persistor.Core.Tests.Internal;
  using DataDuctus.Snmp.Persistor.Core.Tests.TestData;

  using Microsoft.VisualStudio.TestTools.UnitTesting;

  /// <summary>
  /// A set of <see cref="EntitySession.Load{T}"/> tests.
  /// </summary>
  [TestClass]
  public class LoadTests : NetSnmpAgentBasedTest
  {
    #region Public Methods and Operators

    /// <summary>
    /// The test load interfaces.
    /// </summary>
    [TestMethod]
    public void TestLoadInterfaces()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (Interface ifrow in session.Load<Interface>())
        {
          Write(ifrow);
        }
      }
    }

    /// <summary>
    /// The test load ip entries.
    /// </summary>
    [TestMethod]
    public void TestLoadIpEntries()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (InterfaceIpAddress tcpConnection in session.Load<InterfaceIpAddress>())
        {
          Write(tcpConnection);
        }
      }
    }

    /// <summary>
    /// The test load ip entries ordered.
    /// </summary>
    [TestMethod]
    public void TestLoadIpEntriesOrdered()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        IOrderedEnumerable<InterfaceIpAddress> query = from conn in session.Load<InterfaceIpAddress>()
                                                       orderby conn.InterfaceIndex
                                                       select conn;

        foreach (InterfaceIpAddress tcpConnection in query)
        {
          Write(tcpConnection);
        }
      }
    }

    /// <summary>
    /// The test load ip entry for interfaces.
    /// </summary>
    [TestMethod]
    public void TestLoadIpEntryForInterfaces()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        var query = from conn in session.Load<InterfaceIpAddress>()
                    join intf in session.Load<Interface>() on conn.InterfaceIndex equals intf.Index
                    orderby intf.Index
                    select new { intf.Index, intf.Description, conn.Address, NetMask = conn.Netmask };

        foreach (var entry in query)
        {
          Console.WriteLine("[{0}] {1}/{2} {3}", entry.Index, entry.Address, entry.NetMask, entry.Description);
        }
      }
    }

    /// <summary>
    /// The test load media table.
    /// </summary>
    [TestMethod]
    public void TestLoadMediaTable()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (NetMedia service in session.Load<NetMedia>())
        {
          Write(service);
        }
      }
    }

    /// <summary>
    /// The test load routes.
    /// </summary>
    [TestMethod]
    public void TestLoadRoutes()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (Route service in session.Load<Route>())
        {
          Write(service);
        }
      }
    }

    /// <summary>
    /// The test load running windows services.
    /// </summary>
    [TestMethod]
    public void TestLoadRunningWindowsServices()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (WindowsService service in from service in session.Load<WindowsService>()
                                           where service.OpertatingState == OperatingStateTypes.Active
                                           orderby service.Name
                                           select service)
        {
          Console.WriteLine(service.Name);
        }
      }
    }

    /// <summary>
    /// The test load system info.
    /// </summary>
    [TestMethod]
    public void TestLoadSystemInfo()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        SystemInfo sysInfo = session.Load<SystemInfo>().First();

        Write(sysInfo);
      }
    }

    /// <summary>
    /// The test load tcp connections.
    /// </summary>
    [TestMethod]
    public void TestLoadTcpConnections()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (TcpConnection ifrow in session.Load<TcpConnection>())
        {
          Write(ifrow);
        }
      }
    }

    /// <summary>
    /// The test load windows services.
    /// </summary>
    [TestMethod]
    public void TestLoadWindowsServices()
    {
      EntityPersistorFactory factory = Factory.GetEntityFactory();

      using (EntitySession session = factory.OpenSession())
      {
        foreach (WindowsService service in session.Load<WindowsService>())
        {
          Write(service);
        }
      }
    }

    #endregion

    #region Methods

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="ifrow">
    /// The ifrow.
    /// </param>
    private static void Write(Interface ifrow)
    {
      Console.WriteLine("[{0}] {1}", ifrow.Index, ifrow.Description);
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="media">
    /// The media.
    /// </param>
    private static void Write(NetMedia media)
    {
      Console.WriteLine("[{0}:{1}] {2} ({3})", media.Interface, media.Address, media.PhysicalAddress, media.Type);
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="route">
    /// The route.
    /// </param>
    private static void Write(Route route)
    {
      Console.WriteLine(
        "[{0}:{1}/{2}] GW:{3}({4}) A:{5} M:{6}", 
        route.Interface, 
        route.Destination, 
        route.NetMask, 
        route.NextHop, 
        route.Type, 
        route.Age, 
        route.PrimaryMetric);
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="systemInfo">
    /// The system info.
    /// </param>
    private static void Write(SystemInfo systemInfo)
    {
      Console.WriteLine("Name\t\t{0}", systemInfo.Name);
      Console.WriteLine("Description\t{0}", systemInfo.Description);
      Console.WriteLine("Location\t{0}", systemInfo.Location);
      Console.WriteLine("Contact\t\t{0}", systemInfo.Contact);
      Console.WriteLine("UpTime\t\t{0}", systemInfo.UpTime);
      Console.WriteLine("Services\t{0}", systemInfo.Services);
      Console.WriteLine("ObjectId\t{0}", systemInfo.ObjectId);
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="service">
    /// The service.
    /// </param>
    private static void Write(WindowsService service)
    {
      Console.WriteLine(
        "{0} [{1},{2}] U = {3} P = {4}", 
        service.Name, 
        service.InstalledState, 
        service.OpertatingState, 
        service.CanBeUninstalled == UninstallTypes.CanBeUninstalled ? "Y" : "N", 
        service.CanBePaused == PausedTypes.CanBePaused ? "Y" : "N");
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="tcpConnection">
    /// The tcp connection.
    /// </param>
    private static void Write(TcpConnection tcpConnection)
    {
      Console.WriteLine(
        "{0}:{1} -> {2}:{3} [{4}]", 
        tcpConnection.LocalAddress, 
        tcpConnection.LocalPort, 
        tcpConnection.RemoteAddress, 
        tcpConnection.RemotePort, 
        tcpConnection.ConnectionState);
    }

    /// <summary>
    /// The write.
    /// </summary>
    /// <param name="interfaceIpAddress">
    /// The interface ip address.
    /// </param>
    private static void Write(InterfaceIpAddress interfaceIpAddress)
    {
      Console.WriteLine(
        "[{0}] {1}/{2} [BroadCast = {3} ReassSize = {4}]", 
        interfaceIpAddress.InterfaceIndex, 
        interfaceIpAddress.Address, 
        interfaceIpAddress.Netmask, 
        interfaceIpAddress.BroadCastAddress, 
        interfaceIpAddress.MaxSizeReassembly);
    }

    #endregion
  }
}